package com.netposa.datawarehouse.bodysearch.convert;

import com.netposa.datawarehouse.bodysearch.web.model.BodyDownloadCondition;
import com.netposa.datawarehouse.bodysearch.web.vo.BodyVO;
import com.netposa.library.api.IDictionaryCache;
import com.netposa.library.model.Dictionary;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;

public class PccDictionaryConvertImpl implements DictionaryConvert {
   private IDictionaryCache dictionaryCache;

   public PccDictionaryConvertImpl(IDictionaryCache dictionaryCache) {
      this.dictionaryCache = dictionaryCache;
   }

   public String[] convert(BodyVO body, BodyDownloadCondition condition) {
      String gender = "";
      String upperType = "";
      String upperCol = "";
      String lowerType = "";
      String lowCol = "";
      String bodyAppendageStr = "";
      Dictionary dictionary;
      if (body.getGender() != null) {
         dictionary = this.dictionaryCache.get("Sex", body.getGender().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            gender = dictionary.getValue();
         }
      }

      if (body.getUpperType() != null) {
         dictionary = this.dictionaryCache.get("CoatType", body.getUpperType().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            upperType = dictionary.getValue();
         }
      }

      if (body.getUpperColor() != null) {
         dictionary = this.dictionaryCache.get("CoatColor", body.getUpperColor().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            upperCol = dictionary.getValue();
         }
      }

      if (body.getLowerType() != null) {
         dictionary = this.dictionaryCache.get("UnderClothesType", body.getLowerType().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            lowerType = dictionary.getValue();
         }
      }

      if (body.getLowerColor() != null) {
         dictionary = this.dictionaryCache.get("UnderClothesColor", body.getLowerColor().toString());
         if (dictionary != null && StringUtils.isNotBlank(dictionary.getValue())) {
            lowCol = dictionary.getValue();
         }
      }

      Integer bodyAppendageInt = body.getBodyAppendage();
      if (bodyAppendageInt != null) {
         String bodyAppendage = "";
         if (bodyAppendageInt != -1 && bodyAppendageInt != 0) {
            bodyAppendage = this.generateCode(bodyAppendageInt);
         } else {
            bodyAppendage = body.getBagType().toString();
         }

         StringBuffer bodyAppendageBuffer = new StringBuffer();
         String[] var12 = bodyAppendage.split(",");
         int var13 = var12.length;

         for(int var14 = 0; var14 < var13; ++var14) {
            String bodyAppendageTemp = var12[var14];
            Dictionary dictionary2 = this.dictionaryCache.get("BagType_VM", bodyAppendageTemp);
            if (dictionary2 != null && StringUtils.isNotBlank(dictionary2.getValue())) {
               bodyAppendageBuffer.append("/").append(dictionary2.getValue());
            }
         }

         if (bodyAppendageBuffer.length() > 0) {
            bodyAppendageStr = bodyAppendageBuffer.substring(1).toString();
         }
      }

      Date date = new Date(body.getAbsTime());
      SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String absTime = sdFormat.format(date);
      String[] row = new String[]{absTime, body.getDeviceName(), gender, upperType, upperCol, lowerType, lowCol, condition.getDownloadPics() ? body.getSceneImg() : "", bodyAppendageStr};
      return row;
   }

   private String generateCode(int value) {
      String str = "";

      for(int i = 1; i <= 31; ++i) {
         int m = 1 << i - 1;
         if ((m & value) == m) {
            str = str + i + ",";
         }
      }

      return str.substring(0, str.length() - 1);
   }
}
